//
//  informationVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/6/1.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "informationVC.h"
#import "informationCell.h"
#import "informationDetailVC.h"
#import "ConsultCell.h"
@interface informationVC ()<ImagePlayerViewDelegate>{
    NSInteger page ;
}
@property (nonatomic, strong) ImagePlayerView *imagePlayerView;
@property (strong, nonatomic) NSArray * imgArr;// 轮播数组
@property (strong, nonatomic) NSArray * dataArr;// 数据源
@end
@implementation informationVC
#pragma mark - 初始化
- (ImagePlayerView *)imagePlayerView
{
    if (!_imagePlayerView) {
        self.imagePlayerView = [[ImagePlayerView alloc] init];
    }
    return _imagePlayerView;
}

-(NSArray *)imgArr{
    if (!_imgArr ) {
        _imgArr = [NSArray array];
    }
    return _imgArr;
}

-(NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSArray array];
    }
    
    return _dataArr;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.f],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    [self sendRequest];
}

-(void)leftBarButtonItemClick{
    self.imagePlayerView.imagePlayerViewDelegate = nil;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //self.title = self.titleStr;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = self.titleStr;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    page = 1;
    
    // 请求数据
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"page"] = @(page);
    NSString *str;
    if ([self.titleStr isEqualToString:@"PPP资讯"]) {
        
        str = @"getPppNewsList_app_ppp.im";
        
    }else{
        str = @"getPppSuccessList_app_ppp.im";
    }
    [LSHttpTool sendHttpData:str params:dic success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        self.dataArr = dicInfo[@"data"][@"record"];
        self.imgArr = dicInfo[@"data"][@"banner"];
        
        // 设置轮播图
        [self setupAdRollView:self.imgArr];
        
        // 设置TableView
        [self setupTableView];
        
        // 集成下拉加载更多
        [self setupDownRefresh];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"网络连接错误"];

    }];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
 

}
-(void)setupDownRefresh{
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    // 设置刷新图片
    UIImage *images = [UIImage imageNamed:@"mj_footer"];
    NSArray *refreshingImages = @[images];
    [footer setImages:refreshingImages forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多内容了" forState:MJRefreshStateRefreshing];
    footer.stateLabel.font = [UIFont systemFontOfSize:12.0f];
    footer.stateLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0f];
    
    [footer setAutomaticallyRefresh:NO];
    
    self.tableView.mj_footer = footer;
    
}
-(void)loadMoreData{
    
    page++;
    [self sendRequest];
    
}
#pragma mark - 设置轮播图
/**
 *  ad广告图片轮播
 *
 */
- (void)setupAdRollView:(NSArray *)pictureArray{
    self.imagePlayerView.imagePlayerViewDelegate = self;
    
    self.imagePlayerView.scrollInterval = 5.0f;
    
    self.imagePlayerView.pageControlPosition = ICPageControlPosition_BottomRight;
    
    self.imagePlayerView.hidePageControl = NO;
    
    if (pictureArray.count == 1) {
        self.imagePlayerView.pageControl.hidden = YES;
    } else self.imagePlayerView.pageControl.hidden = NO;
    
    self.imagePlayerView.pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    
    self.imagePlayerView.x = 0;
    self.imagePlayerView.y = 0;
    self.imagePlayerView.width = screenW;
    self.imagePlayerView.height = screenH/3;
    self.imagePlayerView.backgroundColor = [UIColor clearColor];
//    self.tableView.tableHeaderView = self.imagePlayerView;
    
//    [self.imagePlayerView reloadData];
}
#pragma mark - ImagePlayerViewDelegate

- (NSInteger)numberOfItems
{
    return self.imgArr.count;
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView loadImageForImageView:(UIImageView *)imageView index:(NSInteger)index
{
    NSString *urlStr = self.imgArr[index];
    [imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"release_logo"]];
    
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView didTapAtIndex:(NSInteger)index
{
    //NSLog(@"did tap index = %d", (int)index);
    
    
}
-(void)setupTableView{
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    [tableView tableViewDisplayWitMsg:@"没有查询到相对应的数据" ifNecessaryForRowCount:_dataArr.count];
    return self.dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 104;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellConsult = @"ConsultCell";
    UINib *nib = [UINib nibWithNibName:@"ConsultCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:cellConsult];
    ConsultCell *cell = [tableView dequeueReusableCellWithIdentifier:cellConsult];
    NSDictionary * dict = self.dataArr[indexPath.section];
    
    NSArray * arr = dict[@"photos"];
    if (arr.count > 0) {
        
        [cell.photo sd_setImageWithURL:dict[@"photos"][0] placeholderImage:[UIImage imageNamed:@"img_normal3"]];
        
    }else{
        cell.photo.image = [UIImage imageNamed:@"img_normal3"];
    }
    cell.titleLabel.text = dict[@"title"];
    
    if ([self.titleStr isEqualToString:@"成功案例"]) {
        cell.webLabel.text = dict[@"caseFrom"];
    }else{
        cell.webLabel.text = dict[@"newsFrom"];
    }
    
    NSDate *date = [CustomExcetion dateFromString:dict[@"createTime"]];
    cell.timeLabel.text = [CustomExcetion prettyDateWithReference:date];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        informationDetailVC *detail = [[informationDetailVC alloc]init];
        detail.newsId = self.dataArr[indexPath.section][@"Id"];
    if ([self.titleStr isEqualToString:@"PPP资讯"]) {
        detail.titleStr = self.dataArr[indexPath.section][@"type"];
    }else{
        detail.titleStr = self.dataArr[indexPath.section][@"caseFrom"];
    }
    
        detail.webUrl = self.dataArr[indexPath.section][@"content"];
        detail.titleType = self.titleStr;
        [self.navigationController pushViewController:detail animated:YES];
    

}
-(void)sendRequest{
    
    // 请求数据
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"page"] = @(page);
    
    NSString *str;
    
    if ([self.titleStr isEqualToString:@"PPP资讯"]) {
        
        str = @"getPppNewsList_app_ppp.im";
        
    }else{
        str = @"getPppSuccessList_app_ppp.im";
    }
    
    [LSHttpTool sendHttpData:str params:dic success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        NSDictionary *dataDic = [NSDictionary dictionary];
        
        dataDic = dicInfo[@"data"];
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            if (page == 1) {
                _dataArr = nil;
                _dataArr = dataDic[@"record"];
                [self.tableView reloadData];
                
            }else{
                NSArray *arr = dataDic[@"record"];
                NSMutableArray * mtArr = [NSMutableArray array];
                for (NSDictionary * dict in _dataArr) {
                    [mtArr addObject:dict];
                }
                for (NSDictionary * dict in arr) {
                    [mtArr addObject:dict];
                }
                _dataArr = nil;
                _dataArr = mtArr;
                [self.tableView reloadData];
            }
            
            [self.tableView.mj_footer endRefreshing];
            
        }else{
            
            [self.tableView.mj_footer endRefreshing];

        }
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:@"网络连接错误"];
        [self.tableView.mj_footer endRefreshing];
    }];

}


@end
